package com.sgb.kjwl.viewmodel.home;

import android.text.TextUtils;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.enterprise.IpPickSelectEntity;
import com.swgk.core.view.entity.EnterpriseEditEntity;
import com.swgk.core.view.entity.HomeManageMenuEntity;
import com.sgb.kjwl.model.entity.home.MenuManageEntity;
import com.sgb.kjwl.repository.HomeRepertory;
import com.sgb.kjwl.view.ui.fragment.home.EnterpriseFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.ArrayList;
import java.util.List;

public class EnterpriseFragModel extends BaseViewModel {
    private EnterpriseFragment mFragment;
    HomeRepertory repertory;

    public EnterpriseFragModel(EnterpriseFragment fragment, HomeRepertory repertory) {
        mFragment = fragment;
        this.repertory = repertory;
    }

    public void getServerTitleLable(){
        APIManager.getInstance().getEnterpriseAPI().getserviceTitleLableForClass1("QiYeFuWu").enqueue(new ICallBack<BaseEntity<MenuManageEntity<List<HomeManageMenuEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<MenuManageEntity<List<HomeManageMenuEntity>>> data) {
                if(mFragment!=null&& data.getData() != null){
                    mFragment.initTopMenu( data.getData());
                }
            }
        });
    }

    int i = 0;
    /**
     * 根据用户身份信息获取展示的企业标题
     */
    public void getCompanyTitleList(String areaCode) {
        i ++;
        areaCode = repertory.getAreaCode();
        SharedPreferenceUtil.getInstance().saveString(SharedPreferenceUtil.AREA_CODE, repertory.getAreaCode());
        int code = TextUtils.isEmpty(areaCode)?615000:Integer.valueOf(areaCode);
        APIManager.getInstance().getBigHomeAPI().getEnterpriseRecListNew(code).enqueue(new ICallBack<BaseEntity<List<EnterpriseEditEntity>>>(true, true) {
            @Override
            public void onSuccess(BaseEntity<List<EnterpriseEditEntity>> data) {
                i = 0;
                mFragment.initCompanyTitle(data.getData());
            }

            @Override
            public void onFailureWithEntity(BaseEntity entity) {
                super.onFailureSwitchErrCode(entity);
                if (entity != null && i<2){
                    if ("53999".equals(entity.getErrCode())){
                        getCompanyTitleList("");
                    }
                }
            }
        });
    }



    public List<IpPickSelectEntity> getTypeData(){
        List<IpPickSelectEntity> list = new ArrayList<>();
        IpPickSelectEntity entity = new IpPickSelectEntity();
        entity.setName("商标需求");
        entity.setId("1");
        list.add(entity);
        IpPickSelectEntity entity1 = new IpPickSelectEntity();
        entity1.setName("专利需求");
        entity1.setId("3");
        list.add(entity1);
        IpPickSelectEntity entity2 = new IpPickSelectEntity();
        entity2.setName("版权需求");
        entity2.setId("2");
        list.add(entity2);
        IpPickSelectEntity entity3 = new IpPickSelectEntity();
        entity3.setName("服务需求");
        entity3.setId("4");
        list.add(entity3);
        return list;
    }

}
